import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { AssetCabinet, ElectricData } from "@/api/backend/index";
import { handleTree } from "@/utils/common";
import qs from "qs";

export const useAssetCabinet = defineStore(Names.AssetCabinet, {
  state: () => {
    return {
      type: <any>[],
      single: <any>{},
      tags: <any>{},
      horizontalFields: <any>[], //卧式机柜字段组合
      typeCategoryFormat: <any>[], //不同类别字段的数据格式
      verticalFields: <any>[], //立式机柜字段组合
      history: "温度",
      beginTime: "",
      endTime: "",
      endData: <any>{}, //获取的历史记录所有字段的数组数据
      xAxisData: <any>[], //x轴坐标
      sites: <any>{}, //站点map
      treeSearchSiteId: <any>[], //侧边栏树待查站点id
      editsiteName: "", //编辑表单待查站点名称
    };
  },
  getters: {},
  actions: {
    //获取机柜管理列表数据
    getAssetCabinetList() {
      return new Promise((resolve, reject) => {
        _get(AssetCabinet + "/list", { pageSize: 100000 }, "json", "")
          .then((res: any) => {
            console.log("获取机柜管理列表数据", res);
            resolve(res.rows);
          })
          .catch((err: any) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //遍历数据分出所有字段数组
    getAllFiledsData(data: any[]) {
      let tempXaxis = <any>[];
      let tempEndData = <any>{};
      for (const key in data[0]) {
        tempEndData[key] = [];
      }
      for (let i = 0, len = data.length; i < len; i++) {
        tempXaxis.push(data[i].createTime);
        for (const key in data[i]) {
          tempEndData[key].push(data[i][key]);
        }
      }
      this.endData = tempEndData;
      this.xAxisData = tempXaxis;
      console.log(
        "机柜管理遍历数据分出所有字段数组",
        this.endData,
        this.xAxisData
      );
      return true;
    },
    //查询机柜历史数据列表
    getElectricData(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          ElectricData +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then(async (res: any) => {
            console.log("查询机柜历史数据列表", res);
            await this.getAllFiledsData(res.rows);
            resolve("");
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //普通过滤机柜数据
    getElectricNormalData(filter: any) {
      return new Promise((resolve, reject) => {
        _get(
          AssetCabinet +
            "/list" +
            "?" +
            qs.stringify(filter, { arrayFormat: "indices" }),
          {},
          "json",
          ""
        )
          .then((res: any) => {
            console.log("普通过滤机柜数据", res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //过滤机柜id
    getInfoById(filter: any) {
      return new Promise((resolve, reject) => {
        _get(AssetCabinet + `/${filter.cabinetId}`, {}, "json", "")
          .then(async (res: any) => {
            console.log("过滤机柜id", res);
            resolve(res.data);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //使用id过滤
    getfilterById(filter: any) {
      return new Promise((resolve, reject) => {
        _get(AssetCabinet + `/${filter.siteId}`, {}, "json", "")
          .then(async (res: any) => {
            console.log("使用id过滤", res);
            resolve(res.data);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
  },
});
